package com.bmob.lover.ui.fragment.album

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.bmob.lover.R
import com.theone.common.constant.BundleConstant
import com.theone.common.ext.getValueNonNull
import com.bmob.lover.data.model.bean.Album
import com.bmob.lover.viewmodel.AlbumViewModel
import com.theone.mvvm.core.app.widge.pullrefresh.PullRefreshLayout
import com.theone.mvvm.core.base.fragment.BaseImageSnapFragment
import com.theone.mvvm.core.databinding.BaseSwipeRefreshFragmentBinding
import com.theone.mvvm.ext.qmui.addLeftCloseImageBtn

//  ┏┓　　　┏┓
//┏┛┻━━━┛┻┓
//┃　　　　　　　┃
//┃　　　━　　　┃
//┃　┳┛　┗┳　┃
//┃　　　　　　　┃
//┃　　　┻　　　┃
//┃　　　　　　　┃
//┗━┓　　　┏━┛
//    ┃　　　┃                  神兽保佑
//    ┃　　　┃                  永无BUG！
//    ┃　　　┗━━━┓
//    ┃　　　　　　　┣┓
//    ┃　　　　　　　┏┛
//    ┗┓┓┏━┳┓┏┛
//      ┃┫┫　┃┫┫
//      ┗┻┛　┗┻┛
/**
 * @author The one
 * @date 2021-06-28 16:51
 * @describe TODO
 * @email 625805189@qq.com
 * @remark
 */
class AlbumListFragment private constructor() :
    BaseImageSnapFragment<Album, AlbumViewModel, BaseSwipeRefreshFragmentBinding>(),
    SwipeRefreshLayout.OnRefreshListener {

    companion object {
        fun newInstance(data: ArrayList<Album>, position: Int, page: Int): AlbumListFragment {
            return AlbumListFragment().apply {
                arguments = Bundle().apply {
                    putParcelableArrayList(BundleConstant.DATA, data)
                    putInt(BundleConstant.POSITION, position)
                    putInt(BundleConstant.PAGE, page)
                }
            }
        }
    }

    private val mAlbumList: ArrayList<Album> by getValueNonNull(BundleConstant.DATA)
    private val mPosition: Int by getValueNonNull(BundleConstant.POSITION)
    private val mPage: Int by getValueNonNull(BundleConstant.PAGE)

    override fun initView(root: View) {
        super.initView(root)
        onFirstLoadSuccess(mAlbumList)
        getRecyclerView().scrollToPosition(mPosition)
        getViewModel().run {
            page = mPage
            isFresh = false
            isFirst = false
        }
        getTopBar()?.run {
            addLeftCloseImageBtn(R.drawable.mz_comment_titlebar_ic_close_light)
            setTitle("${mPosition + 1}/${mAlbumList.size}")
        }
    }

    override fun onLazyInit() {

    }

    override fun onScrollChanged(item: Album, position: Int, total: Int) {
        getSwipeRefreshLayout().isEnabled = position == 0
        getTopBar()?.setTitle("${position + 1}/$total")
    }

    override fun getRecyclerView(): RecyclerView = getDataBinding().recyclerView

    private fun getSwipeRefreshLayout(): SwipeRefreshLayout = getDataBinding().swipeRefresh

    override fun initRefreshView() {
        getSwipeRefreshLayout().run {
            isEnabled = false
            setOnRefreshListener(this@AlbumListFragment)
        }
    }

    override fun onRefresh() {
        getSwipeRefreshLayout().isRefreshing = true
        super.onRefresh()
    }

    override fun setRefreshLayoutEnabled(enabled: Boolean) {
        getSwipeRefreshLayout().run {
            isEnabled = enabled
            isRefreshing = false
        }
    }

    override fun getRefreshLayout(): PullRefreshLayout? = null

}